<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=$this->order_status;
$return['paytype']=get_pay_types();
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
$return['dai_type']=$this->dai_type;
if($_RQ['op']=='list'){
	$this->model('task')->orderup(true);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['order'])){
		$params['order']=trim($_RQ['order']);
		$return['params']['order']=$_RQ['order'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
    //$fields=array('id','order','name','intime','hotelid','roomid','ispay','paytype','fee','mid','createtime','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_jd_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$this->model('model')->uporderstatus($v);
		$v['hotel']=pdo_get('jy_jd_hotel',array('id'=>$v['hotelid']));
		$v['room']=pdo_get('jy_jd_room',array('id'=>$v['roomid']));
		$v['statustitle']=$this->order_status[$v['status']];
		$endtime=strtotime($v['intime'])+$v['day']*24*3600;
		$v['endtime']=date('Y-m-d',$endtime);
		if($v['status']==7){
			$pay=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$v['mid'],'createtime'=>$v['createtime'],'status'=>1));
			if($pay['id']){
				pdo_update('jy_jd_order',array('pay_id'=>$pay['id'],'paytype'=>$pay['pay_type'],'status'=>0,'ispay'=>1,'transid'=>$pay['transaction_id']),array('id'=>$v['id']));
			}
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
			array('op'=>'dai','title'=>'第三方报单'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'order','title'=>'订单号','type'=>'string'),
			array('name'=>'room.title','title'=>'预定的房间','type'=>'string'),
			array('name'=>'num','title'=>'房间数量','type'=>'string'),
			array('type'=>'img_string','title'=>'入住人',
			'data'=>array(
				array('title'=>'入住人：','name'=>'name'),
				array('title'=>'入住电话：','name'=>'mobile')
				)
			),
			array('type'=>'img_string','title'=>'入住时间',
			'data'=>array(
				array('title'=>'入住时间：','name'=>'intime'),
				array('title'=>'离店时间：','name'=>'endtime')
				)
			),
			array('type'=>'img_string','title'=>'费用',
			'data'=>array(
				array('title'=>'订单金额：','name'=>'fee'),
				array('title'=>'包含押金：','name'=>'yafee')
				)
			),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'dai_type','title'=>'预定平台','type'=>'radio','options'=>'dai_type'),			
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'付款方式','type'=>'radio','options'=>'paytype'),	
		),
		'action'=>array(
			array('type'=>'modal','action'=>'jy_jd/order/item','title'=>'订单详情'),
		),),
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'用户昵称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'订单状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='item'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jd_order',array('id'=>$id));
	if(!$item){
		exi('指定订单不存在~','error');
	}
	$item['hotel']=pdo_get('jy_jd_hotel',array('id'=>$item['hotelid']));
	$item['room']=pdo_get('jy_jd_room',array('id'=>$item['roomid']));
	$item['statustitle']=$this->order_status[$item['status']];
	$endtime=strtotime($item['intime'])+$item['num']*24*3600;
	$item['endtime']=date('Y-m-d',$endtime);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'look'=>array(
			array('name'=>'order','title'=>'订单号','type'=>'string'),
			array('name'=>'room.title','title'=>'预定的房间','type'=>'string'),
			array('title'=>'房间数量：','name'=>'num','type'=>'string'),
			array('title'=>'入住人：','name'=>'name','type'=>'string'),
			array('title'=>'入住电话：','name'=>'mobile','type'=>'string'),
			array('title'=>'入住时间：','name'=>'intime','type'=>'string'),
			array('title'=>'离店时间：','name'=>'endtime','type'=>'string'),
			array('title'=>'订单金额：','name'=>'fee','type'=>'string'),
			array('title'=>'订单押金：','name'=>'yafee','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'付款方式','type'=>'radio','options'=>'paytype'),
		),
	);
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['order'])){
		$params['order']=trim($_RQ['order']);
		$return['params']['order']=$_RQ['order'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_jd_order',$params);
	if(count($order)>5000){
		exi('订单数量大于5000，请修改搜索条件！','error');
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$this->model('model')->uporderstatus($v);
		$v['hotel']=pdo_get('jy_jd_hotel',array('id'=>$v['hotelid']));
		$v['room']=pdo_getcolumn('jy_jd_room',array('id'=>$v['roomid']),'title');
		$v['statustitle']=$this->order_status[$v['status']];
		$endtime=strtotime($v['intime'])+$v['day']*24*3600;
		$v['endtime']=date('Y-m-d',$endtime);
		$v['dai_type']=$this->dai_type[$v['dai_type']];
		$v['ispay']=$ispay[$v['ispay']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['createtime']=date('Y-m-d',$v['createtime']);
	}
	$header=array(
		'order'=>'订单号',
		'room'=>'预定的房间',
		'name'=>'入住人',
		'intime'=>'入住时间',
		'endtime'=>'离店时间',
		'fee'=>'费用',
		'statustitle'=>'订单状态',
		'dai_type'=>'预定平台',
		'createtime'=>'下单时间',
		'ispay'=>'是否支付',
		'paytype'=>'付款方式',
	);
	cfc('tools')->export2excel($header,$order,'订单列表');
}
if($_RQ['op']=='del'){
	exi('暂时不支持删除订单！','error');
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jd_order',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jd_order',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>